package avicit.bdp.dds.server.utils;

import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;

public class DataxUtils {

    public static final String DATAX_READER_PLUGIN_MYSQL = "mysqlreader";

    public static final String DATAX_READER_PLUGIN_POSTGRESQL = "postgresqlreader";

    public static final String DATAX_READER_PLUGIN_ORACLE = "oraclereader";

    public static final String DATAX_READER_PLUGIN_SQLSERVER = "sqlserverreader";

    public static final String DATAX_READER_PLUGIN_RDBM = "rdbmsreader";

    public static final String DATAX_READER_PLUGIN_HIVE = "hdfsreader";

    public static final String DATAX_WRITER_PLUGIN_MYSQL = "mysqlwriter";

    public static final String DATAX_WRITER_PLUGIN_POSTGRESQL = "postgresqlwriter";

    public static final String DATAX_WRITER_PLUGIN_ORACLE = "oraclewriter";

    public static final String DATAX_WRITER_PLUGIN_SQLSERVER = "sqlserverwriter";

    public static final String DATAX_WRITER_PLUGIN_RDBM = "rdbmswriter";

    public static final String DATAX_WRITER_PLUGIN_HIVE = "hdfswriter";

    public static final String DATAX_WRITER_PLUGIN_GREENPLUM_WRITER = "gpdbwriter";

    public static String getReaderPluginName(String dbType) {
        switch (dbType) {
            case "mysql":
                return DATAX_READER_PLUGIN_MYSQL;
            case "postgresql":
                return DATAX_READER_PLUGIN_POSTGRESQL;
            case "oracle":
                return DATAX_READER_PLUGIN_ORACLE;
            case "sqlserver":
                return DATAX_READER_PLUGIN_SQLSERVER;
            default:
                return DATAX_READER_PLUGIN_RDBM;
        }
    }

    public static String getWriterPluginName(String dbType) {
        switch (dbType) {
            case "mysql":
                return DATAX_WRITER_PLUGIN_MYSQL;
            case "postgresql":
                return DATAX_WRITER_PLUGIN_POSTGRESQL;
            case "oracle":
                return DATAX_WRITER_PLUGIN_ORACLE;
            case "greenplum":
                return DATAX_WRITER_PLUGIN_GREENPLUM_WRITER;
            case "sqlserver":
                return DATAX_WRITER_PLUGIN_SQLSERVER;
            default:
                return DATAX_WRITER_PLUGIN_RDBM;
        }
    }

    public static SQLStatementParser getSqlStatementParser(String dbType, String sql) {
        switch (dbType) {
            case "mysql":
                return new MySqlStatementParser(sql);
            case "postgresql":
                return new PGSQLStatementParser(sql);
            case "oracle":
                return new OracleStatementParser(sql);
            case "sqlserver":
                return new SQLServerStatementParser(sql);
            case "param":
                return new HiveStatementParser(sql);
            default:
                return null;
        }
    }

    public static String[] convertKeywordsColumns(String dbType, String[] columns) {
        if (columns == null) {
            return null;
        }

        String[] toColumns = new String[columns.length];
        for (int i = 0; i < columns.length; i++ ) {
            toColumns[i] = doConvertKeywordsColumn(dbType, columns[i]);
        }

        return toColumns;
    }

    public static String doConvertKeywordsColumn(String dbType, String column) {
        if (column == null) {
            return column;
        }

        column = column.trim();
        column = column.replace("`", "");
        column = column.replace("\"", "");
        column = column.replace("'", "");

        switch (dbType) {
            case "mysql":
                return String.format("`%s`", column);
            case "postgresql":
                return String.format("\"%s\"", column);
            case "oracle":
                return String.format("\"%s\"", column);
            case "sqlserver":
                return String.format("`%s`", column);
            default:
                return column;
        }
    }

}
